package com.dorukdestan.p2pshare.utiltest;

/**
 * http://www.rgagnon.com/javadetails/java-0490.html
 */

import java.util.*;
import java.io.*;

public class FileWatcher extends TimerTask {
  private long timeStamp;
  private File file;

  public FileWatcher( File file ) {
    this.file = file;
    this.timeStamp = file.lastModified();
  }

  public final void run() {
    long timeStamp = file.lastModified();

    if( this.timeStamp != timeStamp ) {
      this.timeStamp = timeStamp;
      onChange(file);
    }
  }

  protected void onChange( File file ){
	  System.out.println("Changed!");
  }
  
  public static void main(String[] args) {
	  File f = new File("/home/destan/Desktop/xxx");
	    TimerTask task = new FileWatcher(f);

	      Timer timer = new Timer();
	      timer.schedule( task , new Date(), 1000 );

}
}